package com.dlc.shop.delivery.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.app.dto.MyOrderDto;
import com.dlc.shop.bean.app.dto.MyOrderItemDto;
import com.dlc.shop.bean.app.dto.OrderSelfStationDto;
import com.dlc.shop.bean.model.Order;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.delivery.common.model.OrderSelfStation;
import com.dlc.shop.delivery.common.service.OrderSelfStationService;
import com.dlc.shop.security.api.util.SecurityUtils;
import com.dlc.shop.service.MyOrderService;
import com.dlc.shop.service.OrderItemService;
import com.dlc.shop.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Lanhai
 */
@RestController
@RequestMapping("/p/myStationOrder")
@Tag(name = "用户自提订单接口")
@AllArgsConstructor
public class MyStationOrderController {

    private final OrderService orderService;
    private final OrderItemService orderItemService;

    private final MyOrderService myOrderService;



    private final OrderSelfStationService orderSelfStationService;

    @GetMapping("/stationDetail")
    @Operation(summary = "订单详情自提信息" , description = "根据订单号获取订单详情自提信息")
    @Parameter(name = "orderNumber", description = "订单号" , required = true)
    public ServerResponseEntity<OrderSelfStationDto> orderDetail(@RequestParam(value = "orderNumber") String orderNumber, @RequestParam(value = "lat",required = false) Double lat,
                                                                 @RequestParam(value = "lng",required = false) Double lng) {
        OrderSelfStationDto orderSelfStationDto = orderSelfStationService.getStationByOrderNumber(orderNumber, lat, lng);
        return ServerResponseEntity.success(orderSelfStationDto);
    }

    @GetMapping("/getOrderBystationId")
    @Operation(summary = "用户扫自提点码" , description = "用户扫自提点码获取自提订单、虚拟商品订单待核销列表")
    @Parameter(name = "stationId", description = "自提点id" , required = true)
    public ServerResponseEntity<IPage<MyOrderDto>> getOrderListByStationId(PageParam<MyOrderDto> pageParam,@RequestParam(value = "stationId") Long stationId) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<MyOrderDto> page = myOrderService.getOrderListByStationId(pageParam,stationId, userId);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/getOrderByShopId")
    @Operation(summary = "用户扫商家码" , description = "用户扫商家码获取自提订单、虚拟商品订单待核销列表")
    @Parameter(name = "shopId", description = "店铺id" , required = true)
    public ServerResponseEntity<IPage<MyOrderDto>> getOrderListByShopId(PageParam<MyOrderDto> pageParam,@RequestParam(value = "shopId") Long shopId) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<MyOrderDto> page = myOrderService.getOrderListByShopId(pageParam,shopId, userId);
        return ServerResponseEntity.success(page);
    }

    @PutMapping("/orderStationByOrderNumber")
    @Operation(summary = "用户提货操作" , description = "用户提货操作")
    public ServerResponseEntity<Boolean> orderStationByOrderNumber(@RequestBody MyOrderDto myOrderDto) {
        List<String> orderNumberList = Arrays.asList(myOrderDto.getOrderNumbers().split(","));
        return ServerResponseEntity.success(orderService.orderStationByOrderNumber(orderNumberList, SecurityUtils.getUser().getUserId(), myOrderDto.getStationId()));
    }

    @GetMapping("/getStationInfoByOrderNumber")
    @Operation(summary = "我的订单自提信息接口" , description = "根据订单号获取订单详情自提信息")
    @Parameter(name = "orderNumber", description = "订单号" , required = true)
    public ServerResponseEntity<MyOrderDto> getStationDetailByOrderNumber(@RequestParam(value = "orderNumber") String orderNumber) {
        OrderSelfStation orderSelfStation = orderSelfStationService.getOne(new LambdaQueryWrapper<OrderSelfStation>()
                .eq(OrderSelfStation::getOrderNumber, orderNumber));
        OrderSelfStationDto orderSelfStationDto = BeanUtil.map(orderSelfStation, OrderSelfStationDto.class);
        Order order = orderService.getOrderAndOrderItemByOrderNumber(orderNumber);
        //获取订单项的退款信息
        orderService.getOrderItemRefundStatus(order);
        orderItemService.handleActivityOrderItem(order.getOrderItems(),null);
        MyOrderDto myOrderDto = BeanUtil.map(order, MyOrderDto.class);
        myOrderDto.setOrderItemDtos(BeanUtil.mapAsList(order.getOrderItems(), MyOrderItemDto.class));
        myOrderDto.setOrderSelfStationDto(orderSelfStationDto);
        return ServerResponseEntity.success(myOrderDto);
    }
}
